<template>
    <div id="partnerGroup" v-loading="loading">
        <v-tableTitle :btnConfig="btnConfig"
                      :tableSelectData="tableSelectData"
                      :title="'伙伴组'"
                      @del="del" @newCreate="newCreate" @exportFill="exportFill"></v-tableTitle>
        <v-table
                :pageSize="pageSize"
                :page="page"
                :tableConfig="tableConfig"
                :tableData="tableData"
                :total="total"
                @findData="getTableData"
                @getTableData="getTableData"
                @searhData="searhData"
                @selectData="selectData"
                @setdialog="setdialog"
                @upData="getTableData"
                ref="table"
        ></v-table>
    </div>
</template>

<script type="text/ecmascript-6">
    import tableTitle from "commons/TableTitle";
    import table from "commons/Table";
    import baseMixin from "@/mixins/BaseMixin";
    import config from "./Config";
    import bus from "../../../../bus/bus"

    export default {
        name: "partnerGroup",
        mixins: [baseMixin, config],
        data() {
            return {
                loading: false,
                tableConfig: [
                    {
                        label: "伙伴组",
                        prop: "partner_group",
                        input: "input",
                    },
                    {
                        label: this.$t('common.desc'),
                        prop: "partner_group_desc",
                        input: "input",
                    },
                    {
                        label: "伙伴类型",
                        prop: "match_type",
                        input: "select",
                        options: this.getOptions("codelkup/STORERTYPE")
                    },
                ],
                tableData: [],
              page: 1,
                pageSize: 10,
                total: 0,
                tableSelectData: [],
                tableSearhData: {},
                newStatus: true,
                permissionStatus: false,
                wms: ''
            };
        },
        computed: {
            btnConfig() {
                return [
                    {label: this.$t('common.create'), certigier: this.newStatus||!this.btnPermNameList.includes('EN03010401'), invalid: "newCreate"},
                    {label: this.$t('common.delete'), certigier: this.deleteStatus||!this.btnPermNameList.includes('EN03010402'), disabled: true, invalid: "del"},
                    {label: this.$t('common.export'), certigier: this.downStatus||!this.btnPermNameList.includes('EN03010403'), disabled: true, invalid: "exportFill"},
                ]
            }
        },
        created() {
        },
        methods: {
            async exportFill(data) {
                try {
                    let res = await this.$api.post('/downPartnerGroup', data, {responseType: 'blob'})
                    if (window.navigator.msSaveBlob) {
                        var blobObject = new Blob([res.data]);
                        window.navigator.msSaveBlob(blobObject, 'partnergroup.xls');
                    } else {
                        var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document;charset=utf-8'});
                        var filename = 'partnergroup.xls';
                        var downloadElement = document.createElement('a');
                        var href = window.URL.createObjectURL(blob); //创建下载的链接
                        downloadElement.style.display = 'none';
                        downloadElement.href = href;
                        downloadElement.download = filename; //下载后文件名
                        document.body.appendChild(downloadElement);
                        downloadElement.click(); //点击下载
                        document.body.removeChild(downloadElement); //下载完成移除元素
                        window.URL.revokeObjectURL(href); //释放掉blob对象
                    }
                } catch (e) {
                    this.$message.error(e);
                }
            },
            //新建
            async newCreate() {
                this.$store.commit("setNewFlag", true);
                this.$router.push({name: 'partnerGroupDeawer'});
            },
            //选中的内容
            selectData(data) {
                this.tableSelectData = data;
            },
            //删除
            async del(data) {
                try {
                    let res = await this.$api.post(`/deletePartnerGroup`, data);
                    
                        this.$message.success(res.returnMgr);
                        let page = sessionStorage.getItem('page');
                        this.getTableData(page, this.tableSearhData);
                        
                    
                } catch (e) {
                    this.$message.error(e);
                }
            },
            async getTableData(page, data) {
                try {
                    this.loading = true;
                    let res = await this.$api.post(`/queryPartnerGroupList/${this.wms}/${page}`, data);
                    
                        this.tableData = res.page.list;
                        this.pageSize = res.page.pageSize;
                      this.page = res.page.curPage;
                        this.total = res.page.total;
                    
                } catch (e) {
                    this.$message.error(e);
                } finally {
                    this.loading = false;
                }
            },
            //搜索的值
            searhData(data, up) {
                if (!up) {
                    this.tableSearhData = data;
                } else {
                    this.tableSearhData = data;
                    this.getTableData(1, this.tableSearhData);
                }
            },
            //设置弹框
            setdialog(data) {
                this.$store.commit("setRouteData", data);
                this.$router.push({name: 'partnerGroupDeawer'});
            },
        },
        mounted() {
            
            this.wms = this.$store.state.wms;
            this.$store.commit("setSkip", {});
            this.$store.commit("setNewFlag", false);
            this.$store.commit("setRouteData", {});
            this.getTableData(1, {});
        },
        watch: {
            tableSelectData(data) {
                if (this.permissionStatus) {
                    if (data.length > 0) {
                        this.deleteStatus = this.downStatus = false;
                    } else {
                        this.deleteStatus = this.downStatus = true;
                    }
                }
            }
        },
        components: {
            "v-tableTitle": tableTitle,
            "v-table": table,
        }
    };
</script>

<style scoped>
    #partnerGroup {
        width: 100%;
        height: 99%;
        padding-top: 6px;
    }
</style>
